package com.zyb.patient.service;

import org.json.JSONObject;

import com.zyb.patient.dto.UserDto;
import com.zyb.patient.util.Constant;
import com.zyb.patient.util.HttpUtils;
import com.zyb.patient.util.LogUtil;

public class DataService {

	public static final String DATA_SUCCESS = "1";

	/*** 获取应用初始化信息 ***/
	public static UserDto getAPPInfo(Task task) {
		LogUtil.e("getAPPInfo -->");
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				dto = new UserDto();
				dto.result = jo.getString("status");
				if (DATA_SUCCESS.equals(dto.result)) {
					JSONObject data = jo.getJSONObject("arrUserinfo");
					dto.uid = data.getString("uid");
					dto.nickname = data.getString("username");
					dto.userSex = data.getString("sex");// "sex":"m"男 "f"女
					dto.phoneNumber = data.getString("mobileNo");
					dto.userPic = data.getString("userIcon");
					dto.province = data.getString("province");
					dto.city = data.getString("city");
					dto.area = data.getString("area");
					dto.userQrCode = jo.getString("user2dcode");
					dto.versions = jo.getString("versions");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 修改个人资料 ***/
	//
	// URL：{URL}/?s=account/edit_userinfo
	// GET：fieldname（字段名，必填）*、value（修改值，必填）、device_code（设备码，必填）、token（令牌，必填）
	// *字段名：name、sex（f or m）、area（省、市、地区，id用逗号隔开）
	public static UserDto edit_userinfo(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.getString("status");
			// dto.message = jo.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 意见反馈 ***/
	// @
	// URL：{URL}/?s=other/feedback
	// GET：data（反馈内容，必填）、device_code（设备码，必填）、token（令牌，必填）
	public static UserDto sendFeedback(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.getString("status");
			// dto.message = jo.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 取消课程 ***/
	// URL：{URL}/?s=lesson/cancel
	// GET：lession_id（课程id，必填）、device_code（设备码，必填）、token（令牌，必填）
	public static UserDto cancelLesson(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.getString("status");
			// dto.message = jo.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 用户登录 ***/
	public static UserDto login(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			if (info != null) {
				JSONObject jo = new JSONObject(info);
				dto = new UserDto();
				dto.result = jo.getString("status");
				if (DATA_SUCCESS.equals(dto.result)) {
					JSONObject data = jo.getJSONObject("arrUserinfo");
					dto.uid = data.getString("uid");
					dto.nickname = data.getString("username");
					dto.userSex = data.getString("sex");// "sex":"m"男 "f"女
					dto.phoneNumber = data.getString("mobileNo");
					dto.userPic = data.getString("userIcon");
					dto.province = data.getString("province");
					dto.city = data.getString("city");
					dto.area = data.getString("area");
					dto.userQrCode = jo.getString("user2dcode");
					dto.token = jo.getString("token");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

	/*** 用户注册(发送手机号 获取验证码) ***/
	public static UserDto getCode(Task task) {
		UserDto dto = null;
		try {
			String info = HttpUtils.httpGetQuery(Constant.BASE_URL,
					task.getTaskParam());
			JSONObject jo = new JSONObject(info);
			dto = new UserDto();
			dto.result = jo.getString("status");
			if (dto.result.equals("-1"))
				dto.message = jo.getString("errmsg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}

}